package com.lanren.familybookapp.ui.home.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.easyhttp.body.UIProgressResponseCallBack;
import com.lanren.easyhttp.callback.ProgressDialogCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.easyhttp.subsciber.IProgressDialog;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.home.interfaces.ICSVImportModel;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName CSVImportModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/2/13 17:34
 * @Version 1.0
 */
public class CSVImportModel extends BaseModel {

    public CSVImportModel() {
        initDispoable();
    }

    public void bookImport(int bookId, int bookType, String bookMomber, File file, IProgressDialog mProgressDialog,
                           final UIProgressResponseCallBack listener,
                           final ICSVImportModel.CSVImportModel csvImportModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        map.put("bookType", bookType + "");
        map.put("bookMomber", bookMomber);
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_CSV_IMPORT)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .params("file", file, file.getName(), listener)
                .execute(new ProgressDialogCallBack<String>(mProgressDialog, true, false) {

                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            csvImportModel.onSuccess(e.getMessage());
                        } else {
                            csvImportModel.onError(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        csvImportModel.onSuccess(s);
                    }
                });
        addDisposable(disposable);

    }
}
